<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="门店名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入门店名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="博卡门店名称" prop="shopName">
        <el-input
          v-model="queryParams.bokaName"
          placeholder="请输入博卡门店名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>


      <el-form-item label="所属军区" prop="theirMilitary">
        <el-select
          v-model="queryParams.theirMilitary"
          placeholder="请选择所属军区"
        >
          <el-option
            v-for="dict in belong_military_region"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @change="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="具体地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入具体地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:hairFixShop:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:hairFixShop:edit']"
          >修改</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:hairFixShop:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:hairFixShop:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="refresh"
          :loading="syncLoading"
          @click="updateTeacher"
          v-hasPermi="['business:hairFixShop:sync']"
        >同步博卡门店数据</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="hairFixShopList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="门店名称" align="center" prop="shopName" />
      <el-table-column label="博卡门店名称" align="center" prop="bokaName" />
      <el-table-column label="博卡门店编号" align="center" prop="bokaCompId" />
      <el-table-column label="所属军区" align="center" prop="theirMilitary" />
      <el-table-column label="门店类型" align="center" prop="shopType">
        <template #default="scope">
          <div>
            {{ scope.row.shopType === "0" ? "普通店" : "旗舰店" }}
          </div>
        </template>
      </el-table-column>

      <!-- <el-table-column label="门店轮播图" align="center" prop="shopCarousel">
        <template #default="scope">
          <template v-if="scope.row.shopCarousel != null">
            <template
              v-for="item in scope.row.shopCarousel.split(',')"
              :key="item"
            >
              <image-preview :src="item" :width="50" :height="50" />
            </template>
          </template>
        </template>
      </el-table-column> -->
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区" align="center" prop="area" />
      <el-table-column label="简要地址" align="center" prop="address" />
      <el-table-column label="具体地址" align="center" prop="bokaAddress" />
      <!-- <el-table-column
        :label="item.key"
        align="center"
        prop="item.key"
        v-for="item in test"
      >
        <div>
          {{ item.title }}
        </div>
      </el-table-column> -->

      <el-table-column label="门店二维码" align="center" prop="qrCode">
        <template #default="scope">
          <template v-if="scope.row.qrCode != null">
            <template v-for="item in scope.row.qrCode.split(',')" :key="item">
              <image-preview :src="item" :width="50" :height="50" />
            </template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="新客二维码" align="center" prop="qrCode">
        <template #default="scope">
          <template v-if="scope.row.newQrCode != null">
            <template v-for="item in scope.row.newQrCode.split(',')" :key="item">
              <image-preview :src="item" :width="50" :height="50" />
            </template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="老客二维码" align="center" prop="qrCode">
        <template #default="scope">
          <template v-if="scope.row.oldQrCode != null">
            <template v-for="item in scope.row.oldQrCode.split(',')" :key="item">
              <image-preview :src="item" :width="50" :height="50" />
            </template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="推送路线笔记" align="center" prop="shopRouteNotes">
        <template #default="scope">
          <ImagePreview
            :width="100" :height="100"
            :src="scope.row.shopRouteNotes"
            :preview-src-list="[scope.row.shopRouteNotes]"/>
        </template>
      </el-table-column>
      <el-table-column label="推送指引视频" align="center" prop="shopGuideVideo" width="300">
        <template #default="scope">
          <template v-if="scope.row.shopGuideVideo != null">
            <video
              controls
              muted
              :width="200" :height="200"
              type="video/mp4" :src="scope.row.shopGuideVideo">
            </video>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="显示状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_show_hide" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="经度" align="center" prop="lon" /> -->
      <!-- <el-table-column label="纬度" align="center" prop="lat" /> -->
      <!--      <el-table-column label="经纬度" align="center" prop="point" />-->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:hairFixShop:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:hairFixShop:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改补发师门店对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form
        ref="hairFixShopRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="门店名称" prop="shopName">
          <el-input
            v-model="form.shopName"
            placeholder="请输入门店名称"
            style="width: 215px"
          />
        </el-form-item>
        <el-form-item label="门店背景图" prop="shopBackgroundMap">
          <image-uploads v-model="form.shopBackgroundMap"></image-uploads>
        </el-form-item>
        <el-form-item label="所属军区" prop="theirMilitary">
          <el-select v-model="form.theirMilitary" placeholder="请选择所属军区">
            <el-option
              v-for="dict in belong_military_region"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @change="handleQuery"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺类型" prop="shopType">
          <!-- <el-input v-model="form.shopType" placeholder="请选择店铺类型" /> -->
          <el-select v-model="form.shopType" placeholder="请选择店铺类型">
            <el-option
              v-for="dict in shop_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @change="handleQuery"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="门店轮播图" prop="shopCarousel">
          <image-uploads v-model="form.shopCarousel"></image-uploads>
        </el-form-item> -->
        <!-- <el-form-item label="门店二维码" prop="qrCode">
          <image-uploads v-model="form.qrCode" limit="1"></image-uploads>
        </el-form-item> -->
        <el-form-item label="地区选择" prop="regionValue">
          <el-cascader
            v-model="regionValue"
            :options="sysCityListAll"
            :props="{
              children: 'children',
              label: 'label',
              value: 'label',
            }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="简要地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入具体地址" />
        </el-form-item>
        <el-form-item label="推送路线笔记">
         <image-uploads v-model="form.shopRouteNotes" limit="1"></image-uploads>
        </el-form-item>
        <el-form-item label="推送指引视频">
          <video-upload v-model="form.shopGuideVideo"></video-upload>
        </el-form-item>

        <el-form-item label="手动定位" prop="location">
          <el-button open-type="" hover-class="button-hover" @click="openMap">
            点击打开地图
          </el-button>
        </el-form-item>
        <el-form-item label="显示状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_show_hide"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="经度" prop="lon">
          <el-input v-model="form.lon" placeholder="请输入经度" />
        </el-form-item> -->
        <!-- <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 显示地图搜索和定位 -->
    <div class="locations-map" v-if="isMapFlag">
      <locations
        :locationName="form.address"
        @mapClick="handleMapClick"
      ></locations>
    </div>
  </div>
</template>

<script setup name="HairFixShop">
import {
  listHairFixShop,
  getHairFixShop,
  delHairFixShop,
  addHairFixShop,
  updateHairFixShop,
  getProvince,
  updateBokaShop,
} from "@/api/business/hairFixShop";
import { listSystemCityAll } from "@/api/business/systemCity";
import ImageUploads from "@/components/ImageUploads/index.vue";
import locations from "@/components/locations/index.vue";
import VideoUpload from "@/components/VideoUpload/index.vue";

const { proxy } = getCurrentInstance();
const { shop_type,sys_show_hide, belong_military_region } = proxy.useDict(
  "shop_type","sys_show_hide",
  "belong_military_region"
);
const test = ref([
  {
    key: "9: 30",
    title: "xiujian",
  },
  {
    key: "10: 30",
    title: "xiujian",
  },
  {
    key: "11: 30",
    title: "xiujian",
  },
]);
// 地区数据
const regionValue = ref([]);
const hairFixShopList = ref([]);
const sysCityListAll = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const isMapFlag = ref(false);
const syncLoading = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    shopType: undefined,
    shopName: undefined,
    shopCarousel: undefined,
    province: undefined,
    city: undefined,
    area: undefined,
    address: undefined,
    lon: undefined,
    lat: undefined,
    point: undefined,
    theirMilitary: undefined,
  },
  rules: {
    id: [{ required: true, message: "不能为空", trigger: "blur" }],
    shopName: [
      { required: true, message: "门店名称不能为空", trigger: "blur" },
    ],
    theirMilitary: [
      { required: true, message: "所属军区不能为空", trigger: "blur" },
    ],
    shopType: [
      { required: true, message: "店铺类型不能为空", trigger: "blur" },
    ],
    province: [{ required: true, message: "省不能为空", trigger: "blur" }],
    city: [{ required: true, message: "市不能为空", trigger: "blur" }],
    area: [{ required: true, message: "区不能为空", trigger: "blur" }],
    address: [{ required: true, message: "具体地址不能为空", trigger: "blur" }],
    lon: [{ required: true, message: "经度不能为空", trigger: "blur" }],
    lat: [{ required: true, message: "纬度不能为空", trigger: "blur" }],
    point: [{ required: true, message: "经纬度不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

// 选择省
const provinceList = ref([]);
const chooseprovince = async () => {
  provinceList.value = await getProvince();
  console.log("选择省", provinceList.value);
};

// 从子组件获取经纬度
const handleMapClick = (e) => {
  if (e.lat || e.lng) {
    form.value.lon = e.lng;
    form.value.lat = e.lat;
  }
  isMapFlag.value = e.isMapFlag;
};

// 是否显示地图搜索和定位
const openMap = () => {
  // return;
  isMapFlag.value = true;
};

/** 查询补发师门店列表 */
function getList() {
  loading.value = true;
  listHairFixShop(queryParams.value).then((response) => {
    hairFixShopList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function getCityListAll() {
  listSystemCityAll().then((res) => {
    sysCityListAll.value = res.data;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    status: '0',
    shopName: null,
    shopCarousel: null,
    theirMilitary: null,
    province: null,
    city: null,
    area: null,
    shopType: null,
    address: null,
    lon: null,
    lat: null,
    point: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
  };
  regionValue.value = [];
  proxy.resetForm("hairFixShopRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加补发师门店";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true;
  reset();
  const _id = row.id || ids.value;
  getHairFixShop(_id).then((response) => {
    loading.value = false;
    form.value = response.data;
    regionValue.value.push(response.data.province);
    regionValue.value.push(response.data.city);
    regionValue.value.push(response.data.area);
    open.value = true;
    title.value = "修改补发师门店";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hairFixShopRef"].validate((valid) => {
    console.log(form.value);
    // return;
    if (regionValue.length !== 0) {
      form.value.province = regionValue.value[0];
      form.value.city = regionValue.value[1];
      form.value.area = regionValue.value[2];
    }
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateHairFixShop(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
            regionValue.value = [];
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      } else {
        addHairFixShop(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
            regionValue.value = [];
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除补发师门店编号为"' + _ids + '"的数据项？')
    .then(function () {
      loading.value = true;
      return delHairFixShop(_ids);
    })
    .then(() => {
      loading.value = true;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false;
    });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "business/hairFixShop/export",
    {
      ...queryParams.value,
    },
    `hairFixShop_${new Date().getTime()}.xlsx`
  );
}

/** 请求博卡同步接口 */
function updateTeacher() {
  syncLoading.value = true
  updateBokaShop()
    .then((response) => {
      proxy.$modal.msgSuccess("同步成功");
      syncLoading.value = false
      getList();
    })
    .finally(() => {
      syncLoading.value = false
      buttonLoading.value = false;
    });
}

getCityListAll();
getList();
</script>
<style scoped></style>
